package com.ruoyi.teaching.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.TeachingCourseFolderMapper;
import com.ruoyi.teaching.domain.TeachingCourseFolder;
import com.ruoyi.teaching.service.ITeachingCourseFolderService;
import com.ruoyi.common.core.text.Convert;

/**
 * 课程目录Service业务层处理
 *
 * @author William
 * @date 2022-10-11
 */
@Service
public class TeachingCourseFolderServiceImpl implements ITeachingCourseFolderService {
    @Autowired
    private TeachingCourseFolderMapper teachingCourseFolderMapper;

    /**
     * 查询课程目录
     *
     * @param folderId 课程目录主键
     * @return 课程目录
     */
    @Override
    public TeachingCourseFolder selectTeachingCourseFolderByFolderId(Long folderId) {
        return teachingCourseFolderMapper.selectTeachingCourseFolderByFolderId(folderId);
    }

    /**
     * 查询课程目录列表
     *
     * @param teachingCourseFolder 课程目录
     * @return 课程目录
     */
    @Override
    public List<TeachingCourseFolder> selectTeachingCourseFolderList(TeachingCourseFolder teachingCourseFolder) {
        return teachingCourseFolderMapper.selectTeachingCourseFolderList(teachingCourseFolder);
    }

    /**
     * 新增课程目录
     *
     * @param teachingCourseFolder 课程目录
     * @return 结果
     */
    @Override
    public int insertTeachingCourseFolder(TeachingCourseFolder teachingCourseFolder) {
        teachingCourseFolder.setCreateTime(DateUtils.getNowDate());
        return teachingCourseFolderMapper.insertTeachingCourseFolder(teachingCourseFolder);
    }

    /**
     * 修改课程目录
     *
     * @param teachingCourseFolder 课程目录
     * @return 结果
     */
    @Override
    public int updateTeachingCourseFolder(TeachingCourseFolder teachingCourseFolder) {
        teachingCourseFolder.setUpdateTime(DateUtils.getNowDate());
        return teachingCourseFolderMapper.updateTeachingCourseFolder(teachingCourseFolder);
    }

    /**
     * 批量删除课程目录
     *
     * @param folderIds 需要删除的课程目录主键
     * @return 结果
     */
    @Override
    public int deleteTeachingCourseFolderByFolderIds(String folderIds) {
        return teachingCourseFolderMapper.deleteTeachingCourseFolderByFolderIds(Convert.toStrArray(folderIds));
    }

    /**
     * 删除课程目录信息
     *
     * @param folderId 课程目录主键
     * @return 结果
     */
    @Override
    public int deleteTeachingCourseFolderByFolderId(Long folderId) {
        return teachingCourseFolderMapper.deleteTeachingCourseFolderByFolderId(folderId);
    }

    /**
     * 查询课程目录树列表
     *
     * @return 所有课程目录信息
     */
    @Override
    public List<Ztree> selectTeachingCourseFolderTree(TeachingCourseFolder folder) {
        List<TeachingCourseFolder> teachingCourseFolderList = teachingCourseFolderMapper.selectTeachingCourseFolderList(folder);
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (TeachingCourseFolder teachingCourseFolder : teachingCourseFolderList) {
            Ztree ztree = new Ztree();
            ztree.setId(teachingCourseFolder.getFolderId());
            ztree.setpId(teachingCourseFolder.getParentId());
            ztree.setName(teachingCourseFolder.getFolderName());
            ztree.setTitle(teachingCourseFolder.getFolderName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public String getNeedSections(Long courseId, int needSectionCount) {
        if (StringUtils.isNull(courseId)) {
            return "";
        }
        if (StringUtils.isNull(needSectionCount)) {
            return "";
        }
        //还没上课 默认前3节
        Long current = 0L;
        String ancestors = "";
        /**
         * 1、找到单元结构parentId=0
         * 2、找到此第一个单元下的前三节
         * 3、如果不到三节找到第二单元的剩下的章节
         * 4、如果第二单元还是没有继续往下找 直到最后一个单元的最后的章节
         * */
        TeachingCourseFolder param = new TeachingCourseFolder();
        param.setCourseId(courseId);
        int part = 0;
        int unit = 0;
        int hasSectionCount = 0;
        List<Ztree> sections = new ArrayList<>();
        List<Ztree> needParts = new ArrayList<>();
        List<Ztree> needUnits = new ArrayList<>();
        List<Ztree> parts = this.selectTeachingCourseFolderTree(param);
        for (Ztree zp : parts) {
            if (zp.getpId() == 0L) {
                List<Ztree> units = parts.stream().filter(p -> p.getpId().equals(zp.getId())).collect(Collectors.toList());
                needParts.add(zp);
                for (Ztree zc : units) {
                    List<Ztree> section = getChildSection(parts, zc.getId(), (needSectionCount - hasSectionCount));
                    sections.addAll(section);
                    hasSectionCount += sections.size();
                    needUnits.add(zc);
                    //如果找到了足够所需要的小节 就break;
                    if (hasSectionCount >= needSectionCount) {
                        break;
                    }
                    unit++;
                }
                part++;
            }
        }

        if (hasSectionCount <= needSectionCount) {
//            logger.info("课已经上完啦");
        }
        String strPart = StringUtils.join(new Object[]{needParts.stream().map(a -> a.getId().toString()).collect(Collectors.joining(","))});
        String strUnit = StringUtils.join(needUnits.stream().map(a -> a.getId().toString()).collect(Collectors.joining(",")));
        String strSection = StringUtils.join(sections.stream().map(a -> a.getId().toString()).collect(Collectors.joining(",")));
        ancestors = StringUtils.join(strPart, ",", strUnit, ",", strSection);

//        logger.info(String.format("需要的小节ID%s", ancestors));.

        return ancestors;
    }

    private List<Ztree> getChildSection(List<Ztree> total, Long curUnitId, int needCount) {
        List<Ztree> sections = new ArrayList<>();
        List<Ztree> units = total.stream().filter(a -> a.getpId().equals(curUnitId)).collect(Collectors.toList());
        int count = 0;
        for (Ztree zs : units) {
            if (count < needCount) {
                sections.add(zs);
                count++;
            }
        }
        return sections;
    }

    @Override
    public Long getCurrentFolderId(Long courseId, int currentFolderIndex) {
        Long folderId = 0L;
        /**
         * 1、找到单元结构parentId=0
         * 2、找到此第一个单元下的前三节
         * 3、如果不到三节找到第二单元的剩下的章节
         * 4、如果第二单元还是没有继续往下找 直到最后一个单元的最后的章节
         * */
        int cursor = 0;
        TeachingCourseFolder param = new TeachingCourseFolder();
        param.setCourseId(courseId);
        List<Ztree> parts = this.selectTeachingCourseFolderTree(param);
        for (Ztree zp : parts) {
            if (zp.getpId() == 0L) {
                List<Ztree> units = parts.stream().filter(p -> p.getpId().equals(zp.getId())).collect(Collectors.toList());
                for (Ztree zc : units) {
                    List<Ztree> sections = parts.stream().filter(a -> a.getpId().equals(zc.getId())).collect(Collectors.toList());
                    for (Ztree zs : sections) {
                        cursor++;
                        if (cursor == currentFolderIndex) {
                            folderId = zs.getId();
                            break;
                        }
                    }
                }
            }
        }
        return folderId;
    }
}
